package cn.com.headfree.file.controller;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.file.service.ICommonFileService;
import cn.com.headfree.vo.file.DealFileVo;
import feign.Response;

@Controller
@RequestMapping(value = "/commonFileDeal")
public class CommonFileController {
	@Autowired
	private static final Logger LOG = LogManager.getLogger(FileController.class);
	@Autowired
	private ICommonFileService commonFileService;

	@PostMapping(value = "/upload")
	@ResponseBody
	public ResultData upload(@RequestPart(value = "file") MultipartFile file) {
		ResultData result = new ResultData();
//		try {
//			System.out.println("............................");
//			result = commonFileService.upload(file,commonFile);
//			LOG.info("human-manage调用公共组件文件服务上传接口成功");
//		} catch(Exception e) {
//			result.setSuccess(false);
//			result.setErrorMsg("human-manage调用公共组件文件服务上传接口成功异常");
//			LOG.error("human-manage调用公共组件文件服务上传接口成功异常", e);
//		}
		return result;
	}

	@RequestMapping(value = "/downloadFile")
	public void downloadFile(String fileId) {
		ResultData result = new ResultData();
		try {
			Response response = commonFileService.download(fileId);
			LOG.info("human-manage-ding调用human-manage的download成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg("human-manage-ding调用human-manage的download出现异常");
			LOG.error("human-manage-ding调用human-manage的download出现异常", e);
		}
	}
	
	@RequestMapping(value = "/findFileList")
	@ResponseBody
	public ResultData findFileList(@RequestParam(required = true) String sysCode, @RequestParam(required = true) String businessId, String businessNode) {
		ResultData result = new ResultData();
		try {
			result = commonFileService.findFileList(sysCode,businessId,businessNode);
			LOG.info("human-manage-ding调用human-manage的findFileList成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg("human-manage-ding调用human-manage的findFileList出现异常");
			LOG.error("human-manage-ding调用human-manage的findFileList出现异常", e);
		}
		return result;
	}
	
	@RequestMapping(value = "/deleteFile")
	@ResponseBody
	public ResultData deleteFile(@RequestParam(required = true) String fileId) {
		ResultData result = new ResultData();
		try {
			result = commonFileService.deleteFile(fileId);
			LOG.info("human-manage-ding调用human-manage的deleteFile成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg("human-manage-ding调用human-manage的deleteFile出现异常");
			LOG.error("human-manage-ding调用human-manage的deleteFile出现异常", e);
		}
		return result;
	}
	
	@RequestMapping(value = "/getFilesByBusinessId")
	@ResponseBody
	public ResultData getFilesByBusinessId(String businessId){
		ResultData result = new ResultData();
		try {
			result = commonFileService.getFilesByBusinessId(businessId);
			LOG.info("human-manage-ding调用human-manage的getFilesByBusinessId成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg("human-manage-ding调用human-manage的getFilesByBusinessId出现异常");
			LOG.error("human-manage-ding调用human-manage的getFilesByBusinessId出现异常", e);
		}
		return result;
	}
}
